import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import * as topicActions from './topic'

export const refreshTaskSuggestions = taskUserSuggestions => ({
  type: 'REFRESH_TASK_SUGGESTIONS',
  taskUserSuggestions
})

export const refreshSections = sections => ({
  type: 'REFRESH_SECTIONS',
  sections
})

export const refreshTaskInfo = taskInfo => ({
  type: 'REFRESH_TASK',
  taskInfo
})

export const getSections = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/task/${taskId}/sections`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshSections(res.body))
      }
    })()
  }
}

export const editVideoAddress = (taskId, sectionId, videoAddress) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/task/${taskId}/sections`, {taskId, sectionId, videoAddress})
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getSections(taskId))
      }
    })()
  }
}
export const addTask = (programId, topicId, task) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/tasks`, task)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch(topicActions.getAllTopicTask(programId))
      }
    })()
  }
}

export const editTask = (programId, topicId, task) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/tasks/${task.id}`, task)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(topicActions.getAllTopicTask(programId))
      }
    })()
  }
}
export const changeTaskVisible = (visibility, id, programId, topicId) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/tasks/${id}/visibility`, {visibility})
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(topicActions.getAllTopicTask(programId))
      }
    })()
  }
}

export const deleteTask = (programId, topicId, taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/tasks/${taskId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(topicActions.getAllTopicTask(programId))
      }
    })()
  }
}

export const getTask = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshTaskInfo(res.body))
      }
    })()
  }
}

export const getTaskSuggestion = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/tasks/${taskId}/suggestions`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshTaskSuggestions(res.body))
      }
    })()
  }
}

export const dragSort = (data, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/tasks`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(topicActions.getAllTopicTask(programId))
      }
    })()
  }
}

export const getFlipTask = (id) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/task/${id}/flip`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({type: 'FLIP_PAGE_TASK_ID', ids: res.body})
      }
    })()
  }
}
